package com.opdar.athena.message.agent;

import com.opdar.athena.base.Command;
import com.opdar.athena.base.Constants;
import com.opdar.athena.base.MessageType;
import com.opdar.athena.messages.HeartbeatMessage;
import com.opdar.athena.messages.UserMessage;
import com.opdar.athena.registry.ZookeeperRegistry;
import com.opdar.athena.utils.MessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by shiju on 2017/7/2.
 */
public class TransferServer {
    private static TransferServer server = null;
    private ExecutorService executor = Executors.newCachedThreadPool();
    ClassPathXmlApplicationContext context;
    private final static Logger logger = LoggerFactory.getLogger(TransferServer.class);

    public ClassPathXmlApplicationContext getContext() {
        return context;
    }

    public void start() {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServerListener serverListener = context.getBean(ServerListener.class);
        MessageUtil.addMessageType(MessageType.HEARTBEAT, HeartbeatMessage.class);
        MessageUtil.addMessageType(MessageType.USER, UserMessage.class);
        serverListener.connect();
    }

    public static TransferServer getServer() {
        return server;
    }

    public static void main(String[] args) {
        Map<String, String> values = Command.valueOf(args);
        if(values.containsKey("--config")){
            Constants.GLOBAL_PROPERTIES = values.get("--config");
        }
        server = new TransferServer();
        server.start();
    }
}
